#include "Graficador.h"

Graficador* Graficador::instance = 0;

Graficador::Graficador(){
}

Graficador* Graficador::getInstance(){
	if(!Graficador::instance)
		Graficador::instance = new Graficador();
	return Graficador::instance;
}

Graficador::~Graficador(){}

void Graficador::destroy(){
	if(Graficador::instance!=NULL)
		delete Graficador::instance;
	Graficador::instance = NULL;
}

void Graficador::draw(){
	int width = DefaultParameterContainer::getInstance()->getScreenWidth();
	int height = DefaultParameterContainer::getInstance()->getScreenHeight();
	Color *screenColor = new Color(*DefaultParameterContainer::getInstance()->getScreenColor());
	SurfaceContainer *surface = SurfaceContainer::getInstance();
	surface->initializeSurface(width,height,screenColor);

	Texture* texture = DefaultParameterContainer::getInstance()->getTexture();
	if(texture!=NULL){
		FigureContainer::getInstance()->addFigure(new Rectangle("-1",Point(0,height-1),width,height,texture));
	}


	FigureContainer* figures = FigureContainer::getInstance();
	std::map<std::string, Figure*> mapFigures = figures->getAllFigures();
	std::map<std::string, Figure*>::iterator it = mapFigures.begin();
	for(it=mapFigures.begin();it!=mapFigures.end();it++){
		try	{
			Figure* figure = (Figure*)(*it).second;
			figure->draw();
		}catch (DrawingExcep &e){
			Logger::get_instance()->log(e);
		}
	}
	surface->flip();
}
